<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../day33/js/ajax.js"></script>
  </head>
  <body>
    <button>点击我发送请求</button>
    <script>
      let btn = document.querySelector("button");
      btn.onclick = function () {
        // ajax({
        //   url: "get.php",
        // }).then((res) => {
        //   localStorage.setItem("res", res);
        //   let data = JSON.parse(res);
        //   renderDom(data);
        // });
        if (localStorage.getItem("res")) {
          let data = JSON.parse(localStorage.getItem("res"));
          renderDom(data);
        } else {
          ajax({
            url: "get.php",
          }).then((res) => {
            // console.log(res);
            localStorage.setItem("res", res);
            let data = JSON.parse(res);
            renderDom(data);
          });
        }
      };
      function renderDom(data) {
        let divEle = document.createElement("div");
        divEle.innerHTML = `姓名是:${data.name};年龄是${data.age}`;
        document.body.appendChild(divEle);
      }
    </script>
  </body>
</html>
